// @ts-nocheck
bluetooth.startUartService()
bluetooth.startIOPinService()
bluetooth.startButtonService()
bluetooth.startLEDService()
bluetooth.startTemperatureService()
bluetooth.startAccelerometerService()
bluetooth.startMagnetometerService()
bluetooth.setTransmitPower(7)

bootAnimation()

const DEL = 0xFF
const LightLevel = 0x01
const SoundLevel = 0x02
const ServoWrite = 0x03
const IsGesture = 0x04
const CompassHeading = 0x05
const PlayTone = 0x06
const SetVolume = 0x07
const SerovoSetPulse = 0x08

const GestureEvent = 0x6f
const LogoEvent = 0x7F

function uartWriteResponse(num: number, value: number) {
    const low = 0x7F & value
    const high = 0x7F & value >> 7
    bluetooth.uartWriteBuffer(Buffer.fromArray([num, low, high, DEL]))
}

/* BLE Events */
bluetooth.onBluetoothConnected(() => basic.showIcon(IconNames.Happy))
bluetooth.onBluetoothDisconnected(() => basic.showIcon(IconNames.Sad))
/* Logo Events */
input.onLogoEvent(TouchButtonEvent.Pressed, () => uartWriteResponse(LogoEvent, TouchButtonEvent.Pressed))
input.onLogoEvent(TouchButtonEvent.Touched, () => uartWriteResponse(LogoEvent, TouchButtonEvent.Touched))
input.onLogoEvent(TouchButtonEvent.Released, () => uartWriteResponse(LogoEvent, TouchButtonEvent.Released))
input.onLogoEvent(TouchButtonEvent.LongPressed, () => uartWriteResponse(LogoEvent, TouchButtonEvent.LongPressed))
/* Gesture Events */
input.onGesture(Gesture.Shake, () => uartWriteResponse(GestureEvent, Gesture.Shake))
input.onGesture(Gesture.LogoUp, () => uartWriteResponse(GestureEvent, Gesture.LogoUp))
input.onGesture(Gesture.LogoDown, () => uartWriteResponse(GestureEvent, Gesture.LogoDown))
input.onGesture(Gesture.ScreenUp, () => uartWriteResponse(GestureEvent, Gesture.ScreenUp))
input.onGesture(Gesture.ScreenDown, () => uartWriteResponse(GestureEvent, Gesture.ScreenDown))
input.onGesture(Gesture.TiltLeft, () => uartWriteResponse(GestureEvent, Gesture.TiltLeft))
input.onGesture(Gesture.TiltRight, () => uartWriteResponse(GestureEvent, Gesture.TiltRight))
input.onGesture(Gesture.FreeFall, () => uartWriteResponse(GestureEvent, Gesture.FreeFall))
input.onGesture(Gesture.ThreeG, () => uartWriteResponse(GestureEvent, Gesture.ThreeG))
input.onGesture(Gesture.SixG, () => uartWriteResponse(GestureEvent, Gesture.SixG))
input.onGesture(Gesture.EightG, () => uartWriteResponse(GestureEvent, Gesture.EightG))

bluetooth.onUartDataReceived(String.fromCharCode(DEL), function () {
    try {
        const buffer = bluetooth.uartReadBuffer()
        const num = buffer.getUint8(0)

        switch (num) {
            case LightLevel: {
                uartWriteResponse(num, input.lightLevel())
                break
            }
            case SoundLevel: {
                uartWriteResponse(num, input.soundLevel())
                break
            }
            case ServoWrite: {
                const pin = buffer.getUint8(1)
                const low = buffer.getUint8(2)
                const high = buffer.getUint8(3)
                pins.servoWritePin(pin, high << 7 | low)
                break
            }
            case SerovoSetPulse: {
                const pin = buffer.getUint8(1)
                const low = buffer.getUint8(2)
                const high = buffer.getUint8(3)
                pins.servoSetPulse(pin, high << 7 | low)
                break;
            }
            case IsGesture: {
                const gesture = buffer.getUint8(1)
                uartWriteResponse(num, input.isGesture(gesture) ? 1 : 0)
                break
            }
            case CompassHeading: {
                uartWriteResponse(num, input.compassHeading())
                break
            }
            case PlayTone: {
                const low = buffer.getUint8(1)
                const high = buffer.getUint8(2)
                music.ringTone(high << 7 | low)
                break;
            }
            case SetVolume: {
                const low = buffer.getUint8(1)
                const high = buffer.getUint8(2)
                music.setVolume(high << 7 | low)
                break;
            }

        }
    }
    catch (e) {

    }
})

function bootAnimation() {
    led.plot(1, 4)
    led.plot(3, 4)
    basic.pause(75)
    led.unplot(1, 4)
    led.unplot(3, 4)
    led.plot(0, 4)
    led.plot(1, 3)
    led.plot(2, 3)
    led.plot(3, 3)
    led.plot(4, 4)
    basic.pause(75)
    led.unplot(1, 3)
    led.unplot(3, 3)
    led.plot(0, 3)
    led.plot(1, 2)
    led.plot(2, 2)
    led.plot(3, 2)
    led.plot(4, 3)
    basic.pause(75)
    led.unplot(1, 2)
    led.unplot(2, 3)
    led.unplot(3, 2)
    led.plot(0, 1)
    led.plot(0, 2)
    led.plot(1, 1)
    led.plot(3, 1)
    led.plot(4, 1)
    led.plot(4, 2)
    basic.pause(75)
    led.plot(0, 0)
    led.plot(4, 0)
    basic.pause(2000)
    basic.clearScreen()
    basic.showIcon(IconNames.Happy)
    basic.showIcon(IconNames.Sad)
}
